<?php
	defined( "AZ_EXE_" ) or die ( "ERROR : CORE VARIABLE IS NOT DEFINED." );
	class Basic extends AZ {
		public static function fileExist( $fileName ){
			return file_exists( $fileName );
		}
		public static function requireFile( $fileName ){
			if ( Basic::fileExist( $fileName ) ){
				require_once ( $fileName );
			}
			else {
				die ( "ERROR : Can not open file $fileName." );
			}
		}
		public static function copy( $fileSource , $destination ){
			if ( Basic::fileExist( $fileSource ) ){
				copy( $fileSource , $destination );
			}
			else {
				die ( "ERROR : Failed to copy file. File $fileSource does not exist." );
			}
		}
		public static function getVar( $varName ){
			$result = false ;
			if ( isset( $_GET[ $varName ] ) ){
				$result = $_GET[ $varName ];
			}
			else if ( isset( $_POST[ $varName ] ) ){
				$result = $_POST[ $varName ] ;
			}
			return $result ;
		}
		public static function removeFileOrDir( $fileName ){
			$result = false ;
			if ( Basic::fileExist( $fileName ) ){
				$result = is_dir($fileName)?rmdir( $fileName ):unlink($fileName);
			}
			return $result ;
		}
		public static function formatControllerName( $controllerName ){
			return strtoupper( substr( $controllerName , 0 , 1 ) ) . substr( $controllerName , 1 , strlen( $controllerName ) ) . "Controller" ;
		}
		public static function formatModelName( $modelName ){
			return strtoupper( substr( $modelName , 0 , 1 ) ) . substr( $modelName , 1 , strlen( $modelName ) ) . "Model" ;
		}
		public static function formatLanguageName( $languageName ){
			return strtoupper( substr( $languageName , 0 , 1 ) ) . substr( $languageName , 1 , strlen( $languageName ) ) ;
		}
		public static function html_escape($string) {
			return str_replace( array('<','>','“','”',"‘","’",'"',"'") , array('&lt;','&gt;','&ldquo;','&rdquo;','&lsquo;','&rsquo;','&quot;','&apos;'), $string );
		}
		public static function reverse_html_escape($string) {
			
			return str_replace(
					array('<','>','“','”',"‘","’",'"',"'"),
					array('&lt;','&gt;','&ldquo;','&rdquo;','&lsquo;','&rsquo;','&quot;','&apos;'),
					$string);
		}
		public static function getFileInfo($controlName,$sizeMB=5){
			$valid_file = true ;
			//if they DID upload a file...
			if($_FILES[$controlName]['name'])
			{
				//if no errors...
				if(!$_FILES[$controlName]['error'])
				{
					//now is the time to modify the future file name and validate the file
					$new_file_name = strtolower($_FILES[$controlName]['name'] ); //rename file
					if($_FILES[$controlName]['size'] > ((1024000)*$sizeMB)) //can't be larger than 1 MB
					{
						$valid_file = false;
						$message = 'Oops!  Your file\'s size is to large. You can not upload file bigger than ' . $sizeMB . ' MB.';
					}
				}
			}
			return !$valid_file?false:array("name"=>$_FILES[$controlName]['name'],"base64"=>base64_encode($_FILES[$controlName]['name']),"size"=>$_FILES[$controlName]['size'],"type"=>$_FILES[$controlName]['type']);
		}
		public static function uploadFile($controlName,$new_file_name,$sizeMB=5,$folder="resources"){
			$valid_file = true ;
			//if they DID upload a file...
			if($_FILES[$controlName]['name'])
			{
				//if no errors...
				if(!$_FILES[$controlName]['error'])
				{
					//now is the time to modify the future file name and validate the file
					//$new_file_name = strtolower($_FILES[$controlName]['name'] ); //rename file
					if($_FILES[$controlName]['size'] > ((1024000)*$sizeMB)) //can't be larger than 1 MB
					{
						$valid_file = false;
						$message = 'Oops!  Your file\'s size is to large. You can not upload file bigger than ' . $sizeMB . ' MB.';
					}
					//if the file has passed the test
					if($valid_file)
					{
						//move it to where we want it to be
						move_uploaded_file($_FILES[$controlName]['tmp_name'], $folder.'/'.$new_file_name);
						$message = 'Congratulations!  Your file was accepted.';
					}
				}
				//if there is an error...
				else
				{
					//set that to be the returned message
					$message = 'Ooops!  Your upload triggered the following error:  '.$_FILES[$controlName]['error'];
					$valid_file = false ;
				}
			}
			return !$valid_file?false:array("name"=>$_FILES[$controlName]['name'],"base64"=>base64_encode($_FILES[$controlName]['name']),"size"=>$_FILES[$controlName]['size'],"type"=>$_FILES[$controlName]['type']);
		}
		public static function sizeFormat($size){
			$temp=$size/1024;
			if ($temp<1024){
				return (int)$temp . "." . ($size%1024) . " KB" ;
			}else{
				$kb = $size/1024;
				$size= $kb/1024;
				return (int)$size.".".($size%1024). " MB";
			}
		}
		public static function pr($array = array()){
			echo "<pre>";
			print_r($array);
			echo "</pre>";
		}
	}
?>